package mall.service;

import mall.dao.AdminDao;
import mall.dao.AdminDaoImpl;
import mall.model.Admin;
import mall.model.bo.AdminAddAdminBO;
import mall.model.bo.AdminLoginBO;
import mall.model.bo.AdminSearchBO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-27 18:17
 *
 * */
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();

    public Admin login(AdminLoginBO loginBO) {
        Admin admin = new Admin();
        admin.setEmail(loginBO.getEmail());
        admin.setPwd(loginBO.getPwd());
        return adminDao.login(admin);
    }

    public List<Admin> allAdmins() {
        return adminDao.allAdmins();
    }

    public List<Admin> getSearchAdmins(AdminSearchBO searchBO) {
        Admin admin = new Admin();
        admin.setNickname(searchBO.getNickname());
        admin.setEmail(searchBO.getEmail());
        return adminDao.getSearchAdmins(admin);
    }

    public void deleteAdmin(String id) {
        adminDao.deleteAdmin(id);
    }

    public Admin getAdminsInfo(String id) {
        return adminDao.getAdminsInfo(id);
    }

    public Map<String, Object> addAdmin(AdminAddAdminBO addAdminBO) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean matches = matches(addAdminBO);
        if (!matches) {
            return null;
        }
        //查询账号是否重复
        Admin admin = new Admin();
        admin.setEmail(addAdminBO.getEmail());
        Admin result = adminDao.getSearchAdminByEmail(admin);
        //账号重复
        if (result != null){
            map.put("flag", false);
            map.put("content", "该账号不允许重复使用");
            return map;
        }
        //账户信息合法
        admin.setNickname(addAdminBO.getNickname());
        admin.setPwd(addAdminBO.getPwd());
        adminDao.addAdmin(admin);
        map.put("flag", true);
        return map;
    }

    public boolean updateAdmin(Admin admin) {
        AdminAddAdminBO addAdminBO = new AdminAddAdminBO();
        addAdminBO.setEmail(admin.getEmail());
        addAdminBO.setNickname(admin.getNickname());
        addAdminBO.setPwd(admin.getPwd());
        //对账户信息进行正则匹配
        boolean matches = matches(addAdminBO);
        if (!matches) {
            return false;
        }
        adminDao.updateAdmin(admin);
        return true;
    }

    //对账户信息进行正则匹配
    private boolean matches(AdminAddAdminBO addAdminBO) {

        //邮箱匹配，必须qq邮箱 qq号5-10位
        if (!addAdminBO.getEmail().matches("[1-9][0-9]{4,9}@qq.com")) {
            return false;
        }
        //昵称匹配，只能包括数字字母组合，长度为4-15位
        if (!addAdminBO.getNickname().matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,15}$")) {
            return false;
        }
        //密码匹配，最少8位，最多16位，至少一个大写字母，1个小写字母，1个数字和1个特殊字符
        if (!addAdminBO.getPwd().matches("^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,16}$")) {
            return false;
        }
        return true;
    }
}
